package com.jokerx.ohos.fasttrpg.play.slice;

import com.jokerx.ohos.fasttrpg.ResourceTable;
import com.jokerx.ohos.fasttrpg.entity.RoleInfo;
import com.jokerx.ohos.fasttrpg.gamemaster.GameMasterServiceRemoteProxy;
import com.jokerx.ohos.fasttrpg.play.PlayAbility;
import com.jokerx.ohos.fasttrpg.play.PlayConstant;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.IAbilityConnection;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.IntentParams;
import ohos.aafwk.content.Operation;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.LayoutAlignment;
import ohos.bundle.ElementName;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.rpc.IRemoteObject;

import java.util.List;

public class PlayAbilitySlice extends AbilitySlice {
    private static final HiLogLabel TAG = new HiLogLabel(HiLog.DEBUG, 0x0, PlayAbility.class.getName());
    private String localDeviceId;
    private String masterDeviceId;
    private GameMasterServiceRemoteProxy gameMasterServiceRemoteProxy;

    // 创建连接GameMasterService回调实例
    private IAbilityConnection connection = new IAbilityConnection() {
        // 连接到Service的回调
        @Override
        public void onAbilityConnectDone(ElementName elementName, IRemoteObject iRemoteObject, int resultCode) {
            HiLog.info(TAG, "elementName=%{public}s, iRemoteObject=%{public}s, resultCode=%{public}s", elementName, iRemoteObject,
                    resultCode);
            // Client侧需要定义与Service侧相同的IRemoteObject实现类。开发者获取服务端传过来IRemoteObject对象，并从中解析出服务端传过来的信息。
            gameMasterServiceRemoteProxy = new GameMasterServiceRemoteProxy(iRemoteObject);
        }

        // Service异常死亡的回调
        @Override
        public void onAbilityDisconnectDone(ElementName elementName, int resultCode) {
        }
    };

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_play);
        masterDeviceId = intent.getStringParam("masterDeviceId");
        localDeviceId = intent.getOperation().getDeviceId();
        IntentParams intentParams = intent.getParam(PlayConstant.PLAY_ABILITY_SLICE_PARAM);
        int sceneImgSrc = (int) intentParams.getParam(PlayConstant.SCENE_IMG_SRC);
        List<RoleInfo> playerRoleInfos = (List<RoleInfo>) intentParams.getParam(PlayConstant.ROLE_LIST);
        connectGameMaster();
        initView(sceneImgSrc, playerRoleInfos);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    private void initView(int sceneImgSrc, List<RoleInfo> playerRoleInfos) {
        Image scene = (Image) findComponentById(ResourceTable.Id_scene);
        scene.setImageElement(new ShapeElement(this, sceneImgSrc));
        DirectionalLayout roleGrid = (DirectionalLayout)findComponentById(ResourceTable.Id_role_grid);
        playerRoleInfos.stream().forEachOrdered(roleInfo -> {
            DirectionalLayout directionalLayout = new DirectionalLayout(this);
            directionalLayout.setOrientation(Component.VERTICAL);
            directionalLayout.setAlignment(LayoutAlignment.START);
            Text player = new Text(this);
            Text role = new Text(this);
            player.setText(roleInfo.getPlayerId());
            role.setText(roleInfo.getRoleId());
            directionalLayout.addComponent(player);
            directionalLayout.addComponent(role);
            roleGrid.addComponent(directionalLayout);
        });
        Button btnRoll = (Button) findComponentById(ResourceTable.Id_btn_roll);
        btnRoll.setClickedListener(component -> {

        });
    }

    private void connectGameMaster() {
        // 连接Service
        Intent intent = new Intent();
        Operation operation = new Intent.OperationBuilder()
                .withDeviceId(masterDeviceId)
                .withBundleName(getBundleName())
                .withAbilityName("com.jokerx.ohos.fasttrpg.gamemaster.GameMasterServiceAbility")
                .build();
        intent.setOperation(operation);
        connectAbility(intent, connection);
    }
}
